const _ = require('lodash');
const _leven = require('leven');
const parseFullName = require('parse-full-name').parseFullName;

export const getArticleList = list =>
  (list === undefined ? [] : removeExpiredItem(list));

export const getDataList = list =>
  (list === undefined || list === null) ? [] : _.map(list, function(element) {
    return _.extend({}, element, {key: element.id});
  });

export const getDisplayName = fullname => {
  const reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
  let displayName = ''
  if (reg.test(fullname)) {
    displayName = fullname.length > 2 ? fullname.substring(-2, 2) : fullname;
  } else {
    parsedArray = parseFullName(fullname)
    displayName = parsedArray['first'] || parsedArray['last']
  }
  return displayName.replace(/\s/g, '').substring(0, 4)
}

export const getTeamSections = (list, mapping, fresh, key) =>
  (list === undefined || list === null) ? [] : _.map(list, function(element) {
    (fresh = {}) && _.map(element, (value, column) => (key = mapping[column]) &&  _.extend(fresh, {[key]: value}))
    return fresh;
  });

export const filterMembers = (list, property, match)  => {
  let fresh = _.clone(list);
  _.remove(fresh, (item) => ((item[property]) && item[property].match(addSlashes(match)) === null))
  return fresh || [];
}

export const mergeArrayOfObject = (list1, list2, index) => 
  _(list1).keyBy(index)
  .merge(_.keyBy(list2, index))
  .values()
  .value();

export const diffArrayOfObject = (list1, list2, index) => _.differenceBy(list1, list2, index)

export const leven = (string, match) => {
  return _leven(string.toLowerCase(), match.toLowerCase());
}

export const addSlashes = (str) => {
  return str.replace(/&/g, '&amp;') /* This MUST be the 1st replacement. */
  .replace(/'/g, '&apos;') /* The 4 other predefined entities, required. */
  .replace(/"/g, '&quot;')
  .replace(/\\/g, '\\\\')
  .replace(/</g, '&lt;')
  .replace(/>/g, '&gt;').replace(/\u0000/g, '\\0');
}

export const modifyList = (list, id, property, value, handler, notRemoveOther) =>
  (list === undefined || list === null) ? [] : _.map(list, function(element) {
    return (element.id === id || _.isArray(id) && element[id[0]] === id[1]) ? 
      ( handler ? handler(element) : _.extend({}, element, {[property]: value})) 
        : ((!notRemoveOther && delete element[property] && element) || element );
  });

export const removeExpiredItem = (list) => {
  _.remove(list, item => item.expire);
  return list || [];
};

export const removeUncheckedItem = (list) => {
  let fresh = _.clone(list);
  _.remove(fresh, item => !item.checked);
  return fresh || [];
}

export const removeCirclarObject = (list) => {
  let fresh = Object.assign({}, list);
  _.map(fresh, (item, index) => (typeof item === 'object' && !_.isArray(item)) && delete fresh[index]);
  return fresh || [];
}

export const removeCheckedItem = (list) => {
  let fresh = _.clone(list);
  _.remove(fresh, item => item.checked);
  return fresh || [];
}

export const getTypeName = (typeList, typeId) =>
  _.head(_.filter(typeList, o => o.id === typeId.toString())).name;

export const joinWithValue = (list, property, value) => list.map(function(elem){
  return elem[property];
}).join(value || ',');
